package com.crack.nga2.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;

import com.crack.nga2.R;
import com.crack.nga2.custom.HeadLayoutImpl;
import com.crack.nga2.custom.PullListView;
import com.crack.nga2.service.InternetStateService;
import com.crack.nga2.span.BitmapCache;
import com.crack.nga2.span.ImageSpanWrapper;
import com.crack.nga2.span.SpanUtil;
import com.crack.nga2.utils.GlobalConst;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logo);
    }

    /**
     * 初始化全局变量
     */
    private void initGlobalConst() {
        SpanUtil.collapseDrawable = getResources().getDrawable(R.drawable.collapsed_no);
        SpanUtil.collapseDrawable.setBounds(0, 0, 40, 40);
        SpanUtil.listDrawable = getResources().getDrawable(R.drawable.diandian_on);
        SpanUtil.listDrawable.setBounds(0, 0, 20, 20);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        String[] options = getResources().getStringArray(R.array.settings);
        for (int i = 1; i <= 7; i++) {
            String item = options[i];
            boolean b = sp.getBoolean(item, false);
            GlobalConst.settings.put(item, b);
        }
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        GlobalConst.SCREEN_WIDTH = outMetrics.widthPixels;
        GlobalConst.SCREEN_HEIGHT = outMetrics.heightPixels;
        PullListView.MAX_HEIGHT = GlobalConst.SCREEN_HEIGHT / 6;
        HeadLayoutImpl.WIDTH = GlobalConst.SCREEN_WIDTH;

//        readLoginCookie();


        ImageSpanWrapper.defaultDrawable = getResources().getDrawable(R.drawable.loading_img);
        ImageSpanWrapper.failDrawable = getResources().getDrawable(R.drawable.default_pic);
        setDrawable(ImageSpanWrapper.defaultDrawable);
        ImageSpanWrapper.failDrawable.setBounds(0, 0, 150, 120);
    }

    private void readLoginCookie() {
        File file = new File(getFilesDir(), "cookie.cache");
        if (file.exists()) {
            try {
                FileReader reader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(reader);
                GlobalConst.Cookie = bufferedReader.readLine();
                GlobalConst.NO_LOGIN_COOKIE = GlobalConst.Cookie;
                bufferedReader.close();
                GlobalConst.isLogin = true;
                //获取uid
                Pattern p = Pattern.compile("(?<=ngaPassportUid=)(\\d+?)(?=;)");
                Matcher m = p.matcher(GlobalConst.Cookie);
                if (m.find()) {
                    GlobalConst.UID = Integer.parseInt(m.group());
                }
                //获取用户名
                p = Pattern.compile("(?<=ngaPassportUrlencodedUname=)(.+?)(?=;)");
                m = p.matcher(GlobalConst.Cookie);
                if (m.find()) {
                    GlobalConst.username = URLDecoder.decode(URLDecoder.decode(m.group(), "GBK"), "GBK");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void setDrawable(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        float f = width * 1.0f / GlobalConst.SCREEN_WIDTH;
        height = (int) (height * 1.0 / f);
        width = GlobalConst.SCREEN_WIDTH;
        drawable.setBounds(0, 0, width, height);
    }


    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(LogoActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 1000);
//        initGlobalConst();
//        initEmotions();
       /* //初始化图片加载配置
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .threadPoolSize(2)
                .memoryCacheSize(15 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                .defaultDisplayImageOptions(options)
                .build();

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);*/
    }
    private void initEmotions() {
        Map<String, String> emotions = GlobalConst.emotions;
        emotions.put("ac:blink", "[s:ac:blink]");
        emotions.put("ac:cahan", "[s:ac:擦汗]");
        emotions.put("ac:cha", "[s:ac:茶]");
        emotions.put("ac:chaoxiao", "[s:ac:嘲笑]");
        emotions.put("ac:chaoxiao1", "[s:ac:嘲笑1]");
        emotions.put("ac:chou", "[s:ac:愁]");
        emotions.put("ac:chuan", "[s:ac:喘]");
        emotions.put("ac:dai", "[s:ac:呆]");
        emotions.put("ac:e", "[s:ac:哦]");
        emotions.put("ac:fangdui", "[s:ac:反对]");
        emotions.put("ac:goodjob", "[s:ac:goodjob]");
        emotions.put("ac:han", "[s:ac:汗]");
        emotions.put("ac:heiqiang", "[s:ac:黑枪]");
        emotions.put("ac:heng", "[s:ac:哼]");
        emotions.put("ac:huachi", "[s:ac:花痴]");
        emotions.put("ac:jihuatong", "[s:ac:计划通]");
        emotions.put("ac:jing", "[s:ac:惊]");
        emotions.put("ac:jiong", "[s:ac:囧]");
        emotions.put("ac:koubi", "[s:ac:抠鼻]");
        emotions.put("ac:ku", "[s:ac:酷]");
        emotions.put("ac:ku1", "[s:ac:酷1]");
        emotions.put("ac:kuxiao", "[s:ac:哭笑]");
        emotions.put("ac:leng", "[s:ac:冷]");
        emotions.put("ac:lingluan", "[s:ac:凌乱]");
        emotions.put("ac:o", "[s:ac:哦]");
        emotions.put("ac:pa", "[s:ac:怕]");
        emotions.put("ac:pen", "[s:ac:喷]");
        emotions.put("ac:shang", "[s:ac:上]");
        emotions.put("ac:shanguang", "[s:ac:闪光]");
        emotions.put("ac:shuai", "[s:ac:摔]");
        emotions.put("ac:touxiao", "[s:ac:偷笑]");
        emotions.put("ac:weiqu", "[s:ac:委屈]");
        emotions.put("ac:wen", "[s:ac:问]");
        emotions.put("ac:wuyu", "[s:ac:无语]");
        emotions.put("ac:xia", "[s:ac:吓]");
        emotions.put("ac:xianmu", "[s:ac:羡慕]");
        emotions.put("ac:xiaren", "吓人");
        emotions.put("ac:xiu", "[s:ac:羞]");
        emotions.put("ac:yi", "[s:ac:咦]");
        emotions.put("ac:youshang", "[s:ac:忧伤]");
        emotions.put("ac:yun", "[s:ac:晕]");
        emotions.put("ac:zantong", "[s:ac:赞同]");
        emotions.put("ac:zhongqiang", "[s:ac:中枪]");
        emotions.put("ac:zhuakuang", "[s:ac:抓狂]");
        /* ========================= */
        emotions.put("dt:aojiao", "[s:dt:傲娇]");
        emotions.put("dt:bishi", "[s:dt:鄙视]");
        emotions.put("dt:chaoxiao", "[s:dt:嘲笑]");
        emotions.put("dt:chachuqu", "[s:dt:叉出去]");
        emotions.put("dt:faguang", "[s:dt:发光]");
        emotions.put("dt:haqian", "[s:dt:呵欠]");
        emotions.put("dt:heilian", "[s:dt:黑脸]");
        emotions.put("dt:jing", "[s:dt:惊]");
        emotions.put("dt:jing2", "[s:dt:惊2]");
        emotions.put("dt:kengutou", "[s:dt:啃骨头]");
        emotions.put("dt:ku", "[s:dt:哭]");
        emotions.put("dt:lei", "[s:dt:雷]");
        emotions.put("dt:lei2", "[s:dt:雷2]");
        emotions.put("dt:lianhong", "[s:dt:脸红]");
        emotions.put("dt:nu", "[s:dt:怒]");
        emotions.put("dt:nu2", "[s:dt:怒2]");
        emotions.put("dt:ROLL", "[s:dt:ROLL]");
        emotions.put("dt:shang", "[s:dt:上]");
        emotions.put("dt:shao", "[s:dt:烧]");
        emotions.put("dt:shuai", "[s:dt:摔]");
        emotions.put("dt:wuyu", "[s:dt:无语]");
        emotions.put("dt:xian", "[s:dt:闲]");
        emotions.put("dt:xiao", "[s:dt:笑]");
        emotions.put("dt:xiao2", "[s:dt:笑2]");
        emotions.put("dt:xiao3", "[s:dt:笑3]");
        emotions.put("dt:xin", "[s:dt:心]");
        emotions.put("dt:xingxingyan", "[s:dt:星星眼]");
        emotions.put("dt:xingxingyan2", "[s:dt:星星眼2]");
        emotions.put("dt:yao", "[s:dt:药]");
        emotions.put("dt:yuan", "[s:dt:怨]");
        emotions.put("dt:yun", "[s:dt:晕]");
        emotions.put("dt:zhuyi", "[s:dt:注意]");
        emotions.put("dt:zhuyi2", "[s:dt:注意2]");
         /* ========================= */
        emotions.put("pst:anwei", "[s:pst:安慰]");
        emotions.put("pst:bei", "[s:pst:背]");
        emotions.put("pst:bixue", "[s:pst:鼻血]");
        emotions.put("pst:can", "[s:pst:惨]");
        emotions.put("pst:chaoxiao", "[s:pst:嘲笑]");
        emotions.put("pst:dai", "[s:pst:呆]");
        emotions.put("pst:dai2", "[s:pst:呆2]");
        emotions.put("pst:dai3", "[s:pst:呆3]");
        emotions.put("pst:dianji", "[s:pst:电击]");
        emotions.put("pst:faguang", "[s:pst:发光]");
        emotions.put("pst:fu", "[s:pst:扶]");
        emotions.put("pst:haqian", "[s:pst:呵欠]");
        emotions.put("pst:heilian", "[s:pst:黑脸]");
        emotions.put("pst:huaiyi", "[s:pst:怀疑]");
        emotions.put("pst:huoxing", "[s:pst:火星]");
        emotions.put("pst:ji", "[s:pst:基]");
        emotions.put("pst:jingdai", "[s:pst:惊呆]");
        emotions.put("pst:jingdai2", "[s:pst:惊呆2]");
        emotions.put("pst:jingdai3", "[s:pst:惊呆3]");
        emotions.put("pst:jingxia", "[s:pst:惊吓]");
        emotions.put("pst:jingxia2", "[s:pst:惊吓2]");
        emotions.put("pst:jinzhang", "[s:pst:紧张]");
        emotions.put("pst:jushou", "[s:pst:举手]");
        emotions.put("pst:kaixin", "[s:pst:开心]");
        emotions.put("pst:kaixin2", "[s:pst:开心2]");
        emotions.put("pst:kaixin3", "[s:pst:开心3]");
        emotions.put("pst:kanxi", "[s:pst:看戏]");
        emotions.put("pst:ku", "[s:pst:哭]");
        emotions.put("pst:ku2", "[s:pst:哭2]");
        emotions.put("pst:ku3", "[s:pst:哭3]");
        emotions.put("pst:lei", "[s:pst:泪]");
        emotions.put("pst:lei2", "[s:pst:泪2]");
        emotions.put("pst:lei3", "[s:pst:泪3]");
        emotions.put("pst:lei4", "[s:pst:泪4]");
        emotions.put("pst:lianhong", "[s:pst:脸红]");
        emotions.put("pst:lianhong2", "[s:pst:脸红2]");
        emotions.put("pst:manzu", "[s:pst:满足]");
        emotions.put("pst:manzu2", "[s:pst:满足2]");
        emotions.put("pst:meiwei", "[s:pst:美味]");
        emotions.put("pst:men", "[s:pst:闷]");
        emotions.put("pst:men2", "[s:pst:闷2]");
        emotions.put("pst:nu", "[s:pst:怒]");
        emotions.put("pst:nu2", "[s:pst:怒2]");
        emotions.put("pst:ou", "[s:pst:呕]");
        emotions.put("pst:qin", "[s:pst:亲]");
        emotions.put("pst:shayan", "[s:pst:傻眼]");
        emotions.put("pst:shayan2", "[s:pst:傻眼2]");
        emotions.put("pst:touxiao", "[s:pst:偷笑]");
        emotions.put("pst:touxiao2", "[s:pst:偷笑2]");
        emotions.put("pst:touxiao3", "[s:pst:偷笑3]");
        emotions.put("pst:tuzi", "[s:pst:兔子]");
        emotions.put("pst:xiaoerbuyu", "[s:pst:笑而不语]");
        emotions.put("pst:xie", "[s:pst:谢]");
        emotions.put("pst:xieyan", "[s:pst:斜眼]");
        emotions.put("pst:xingfu", "[s:pst:幸福]");
        emotions.put("pst:xingxingyan", "[s:pst:星星眼]");
        emotions.put("pst:yandai", "[s:pst:眼袋]");
        emotions.put("pst:yanjing", "[s:pst:眼镜]");
        emotions.put("pst:yateng", "[s:pst:牙疼]");
        emotions.put("pst:yinyue", "[s:pst:音乐]");
        emotions.put("pst:yuan", "[s:pst:怨]");
        emotions.put("pst:yun", "[s:pst:晕]");
        emotions.put("pst:zhai", "[s:pst:宅]");
        emotions.put("pst:zui", "[s:pst:醉]");
    }
}
